package com.ruoyi.wms.service.impl;

import java.util.List;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.MD5Utils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.wms.mapper.StockTransferMapper;
import com.ruoyi.wms.domain.StockTransfer;
import com.ruoyi.wms.service.IStockTransferService;

/**
 * 库存调拨Service业务层处理
 *
 * @author ruoyi
 * @date 2025-10-24
 */
@Service
public class StockTransferServiceImpl implements IStockTransferService {
    @Autowired
    private StockTransferMapper stockTransferMapper;

    /**
     * 查询库存调拨
     *
     * @param id 库存调拨主键
     * @return 库存调拨
     */
    @Override
    public StockTransfer selectStockTransferById(String id) {
        return stockTransferMapper.selectStockTransferById(id);
    }

    /**
     * 查询库存调拨列表
     *
     * @param stockTransfer 库存调拨
     * @return 库存调拨
     */
    @Override
    public List<StockTransfer> selectStockTransferList(StockTransfer stockTransfer) {
        return stockTransferMapper.selectStockTransferList(stockTransfer);
    }

    /**
     * 新增库存调拨
     *
     * @param stockTransfer 库存调拨
     * @return 结果
     */
    @Override
    public int insertStockTransfer(StockTransfer stockTransfer) {
        stockTransfer.setId(MD5Utils.md5(stockTransfer.getTransferNum()));
        stockTransfer.setCreateUser(SecurityUtils.getUsername());
        stockTransfer.setCreateDate(DateUtils.getCurrentTime());
        stockTransfer.setUpdateDate( DateUtils.getCurrentTime());

        return stockTransferMapper.insertStockTransfer(stockTransfer);
    }

    /**
     * 修改库存调拨
     *
     * @param stockTransfer 库存调拨
     * @return 结果
     */
    @Override
    public int updateStockTransfer(StockTransfer stockTransfer) {

        StockTransfer vo = stockTransferMapper.selectStockTransferById(stockTransfer.getId());
        int updateResult = 0;
        if (vo==null || StringUtils.isEmpty(stockTransfer.getId())) {
            updateResult = insertStockTransfer(stockTransfer);
            return updateResult;
        }
        stockTransfer.setUpdateDate( DateUtils.getCurrentTime());
        updateResult =  stockTransferMapper.updateStockTransfer(stockTransfer);
        if (0 == updateResult) {
            updateResult =  stockTransferMapper.updateStockTransfer(stockTransfer);
        }

        return updateResult;


    }

    /**
     * 批量删除库存调拨
     *
     * @param ids 需要删除的库存调拨主键
     * @return 结果
     */
    @Override
    public int deleteStockTransferByIds(String[] ids) {
        return stockTransferMapper.deleteStockTransferByIds(ids);
    }

    /**
     * 删除库存调拨信息
     *
     * @param id 库存调拨主键
     * @return 结果
     */
    @Override
    public int deleteStockTransferById(String id) {
        return stockTransferMapper.deleteStockTransferById(id);
    }
}
